home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / WP.C < prev   
C/C++ Source or Header  |  1991-02-04  |  18KB  |  717 lines

  1. #include "pt.h"
  2. #include "string.h"
  3. #include "direct.h"
  4.  
  5. #define PATTERNSIZE 64
  6.  
  7. static unsigned char *endPattern;
  8. static unsigned char *beginPattern;
  9. static unsigned char filePatterns[PATTERNSIZE];
  10. static unsigned char retFilename[PATTERNSIZE];
  11. static int savedAttribute;
  12. static int dirNameSize;
  13.  
  14. unsigned char * pascal
  15. /* XTAG:findFirstPatternFile */
  16. findFirstPatternFile(patterns, attribute, dta)
  17.     unsigned char *patterns;
  18.     int attribute;
  19.     unsigned char *dta;
  20. {
  21.     unsigned char *fileName;
  22.     unsigned char *endPrefix;
  23.     int i;
  24.  
  25.     /* save patterns for later use in findNextPatternFile */
  26.     strncpy(filePatterns, patterns, PATTERNSIZE);
  27.     savedAttribute = attribute;
  28.     beginPattern = filePatterns;
  29.  
  30.     /* see if this pattern has at least two components */
  31.     endPattern = strchr(beginPattern, '|');
  32.     
  33.     /* find the path prefix and isolate the final pathname component */
  34.     /* NOTE: this fails for full pathnames (starting with "\") */
  35.     /* start looking at the end of the pathname */
  36.     if( endPattern != NULL ) {
  37.         *endPattern = '\0';
  38.         endPrefix = endPattern - 1;
  39.     } else
  40.         endPrefix = beginPattern + strlen(filePatterns) - 1;
  41.  
  42.     /* look until you see a "\" or a ":" */
  43.     for(  ; endPrefix >= beginPattern; --endPrefix)
  44.         if( *endPrefix == ':' || *endPrefix == '\\' )
  45.             break;
  46.     dirNameSize = endPrefix - beginPattern + 1;
  47.  
  48.     /* start looking for matching file names */
  49.     fileName = findFirstFile(beginPattern, savedAttribute, dta);
  50.     
  51.     /* skip "." */
  52.     if( fileName == NULL || strcmp(&fileName[30], ".") == 0 )
  53.         return findNextPatternFile(dta);
  54.  
  55.     /* else contruct the filename */
  56.     for(i = 0; i < dirNameSize; ++i)
  57.         retFilename[i] = beginPattern[i];
  58.     retFilename[i] = '\0';
  59.     strcat(retFilename, &fileName[30]);
  60.     return retFilename;
  61. }
  62.     
  63. unsigned char * pascal
  64. /* XTAG:findNextPatternFile */
  65. findNextPatternFile(dta)
  66.     unsigned char *dta;
  67. {
  68.     unsigned char *fileName;
  69.     unsigned char *endPrefix;
  70.     register int i;
  71.  
  72.     /* find the next matching file name */
  73.     fileName = findNextFile(dta);
  74.     if( fileName != NULL )
  75.         goto ret;
  76.  
  77. tryMorePatterns:
  78.     /* see if this pattern has two or more components left */
  79.     if( endPattern != NULL )
  80.         *endPattern++ = '|';    /* restore pattern as it was */
  81.     else    /* no more file patterns to expand */
  82.         return NULL;
  83.  
  84.     /* next pattern begin where the last one ends */
  85.     beginPattern = endPattern;
  86.     endPattern = strchr(beginPattern, '|');
  87.     if( endPattern != NULL ) {
  88.         *endPattern = '\0';
  89.         endPrefix = endPattern - 1;
  90.     } else
  91.         endPrefix = beginPattern + strlen(beginPattern) - 1;
  92.  
  93.     for(  ; endPrefix >= beginPattern; --endPrefix)
  94.         if( *endPrefix == ':' || *endPrefix == '\\' )
  95.             break;
  96.     dirNameSize = endPrefix - beginPattern + 1;
  97.  
  98.     /* start looking for matching file names */
  99.     fileName = findFirstFile(beginPattern, savedAttribute, dta);
  100.     if( fileName == NULL )
  101.         goto tryMorePatterns;
  102.  
  103. ret:
  104.     /* else contruct the filename */
  105.     for(i = 0; i < dirNameSize; ++i)
  106.         retFilename[i] = beginPattern[i];
  107.     retFilename[i] = '\0';
  108.     strcat(retFilename, &fileName[30]);
  109.     return retFilename;
  110. }
  111.  
  112.  
  113. unsigned char * pascal
  114. /* XTAG:makeFullPathname */
  115. makeFullPathname(origName)
  116.     unsigned char *origName;
  117. {
  118.     extern unsigned char msgBuffer[];
  119.     extern unsigned char scratchFileName[];
  120.  
  121.     int offset, n;
  122.     register unsigned char *p;
  123.     unsigned char *fromPtr, *toPtr, ch;
  124.  
  125.     /* figure out the complete pathname for the file */
  126.     offset = 0;    /* offset into origName */
  127.  
  128.     /* first figure out what we have to do */
  129.     if( origName[1] == ':' ) {
  130.         if( origName[2] != '\\' && origName[2] != '/' ) {
  131.             /* get the current directory on that drive */
  132.             n = getDefaultDrive();    /* save default drive */
  133.             (void)setDefaultDrive(toupper(origName[0])-'A');
  134.  
  135.             /* get the current directory on that drive */
  136.             (void)getcwd(&msgBuffer[0], MSGBUFFERSIZE);
  137.  
  138.             /* restore the orignal defualt drive */
  139.             (void)setDefaultDrive(n);
  140.  
  141.             /* skip the drive spec since getcwd gets it */
  142.             offset = 2;
  143.             goto fixDirectoryName;
  144.  
  145.         } else    /* name begins A:\ or something like it so */
  146.             /* just use the string typed in */
  147.             scratchFileName[0] = '\0';
  148.  
  149.     } else if( origName[0] == '\\' || origName[0] == '/' ) {
  150.         /* add a drive identifier to the string typed in */
  151.         scratchFileName[0] = (unsigned char)'A' + getDefaultDrive();
  152.         scratchFileName[1] = ':';
  153.         scratchFileName[2] = '\0';
  154.  
  155.     } else {    /* must prepend the current drive and directory */
  156.         (void)getcwd(&msgBuffer[0], MSGBUFFERSIZE);
  157.     fixDirectoryName:
  158.         strcpy(scratchFileName, msgBuffer);
  159.         n = strlen(scratchFileName);
  160.         if( scratchFileName[n-1] != '\\' ) {
  161.             scratchFileName[n++] = '\\';
  162.             scratchFileName[n] = '\0';
  163.         }
  164.     }
  165.     strncat(scratchFileName, &(origName[offset]), FILENAMESIZE);
  166.     
  167.     /* now eliminate any ".." components */
  168.     p = &scratchFileName[0];
  169.     while( *p != '\0' ) {
  170.         /* look for a "\..\" */
  171.         if( *p == '.' && *(p+1) == '.'
  172.             && *(p-1) == '\\' && *(p+2) == '\\' ) {
  173.             /* find the previous path component */
  174.             n = 2;
  175.             while( 1 ) {
  176.                 if( (p-n) < &scratchFileName[0] )
  177.                     /* string is "component\..\ ..." */
  178.                     break;
  179.                 ch = *(p-n);
  180.                 if( ch == '\\' || ch == ':' )
  181.                     break;
  182.                 ++n;
  183.             }
  184.             /* eliminate the "component\..\" by copying the */
  185.             /* rest of the  string up n character positions */
  186.             fromPtr = p + 3;
  187.             /* *(p-n) is the last character to keep so: */
  188.             toPtr = p - n + 1;
  189.             /* move p to continue the scan at the beginning */
  190.             /* of the moved part of the string */
  191.             p = toPtr;
  192.             while( 1 ) {
  193.                 *toPtr = *fromPtr++;
  194.                 if( *toPtr++ == '\0' )
  195.                     break;
  196.             }
  197.         } else
  198.             ++p;
  199.     }
  200.  
  201.     return &scratchFileName[0];
  202. }
  203.  
  204.  
  205. struct window * pascal
  206. /* XTAG:findFilenameWindow */
  207. findFilenameWindow(filename)
  208.     unsigned char *filename;
  209. {
  210.     extern struct window *windowList;
  211.     extern struct openFile *files;
  212.  
  213.     register struct window *w;
  214.  
  215.     w = windowList;
  216.     while( w != NULL ) {
  217.         if( stricmp(files[w->fileId].origName, filename) == 0 )
  218.             return w;
  219.         w = w->nextWindow;
  220.     }
  221.     return NULL;
  222. }
  223.  
  224.  
  225. unsigned char * pascal
  226. /* XTAG:findFirstFile */
  227. findFirstFile(pattern, attribute, dta)
  228.     unsigned char *pattern;
  229.     int attribute;
  230.     unsigned char *dta;
  231. {
  232.     extern union REGS rin, rout;
  233.  
  234.     /* set the disk tranfer address */
  235.     rin.h.ah = 0x1A;
  236.     rin.x.dx = (int)dta;
  237.     intdos(&rin, &rout);
  238.     
  239.     /* start looking for matching file names */
  240.     rin.h.ah = 0x4E;
  241.     rin.x.cx = attribute;    /* search attribute */
  242.     rin.x.dx = (int)pattern;
  243.     intdos(&rin, &rout);
  244.  
  245.     if( (rout.x.cflag & 1) == 1 )
  246.         return NULL;
  247.     else
  248.         return dta;
  249. }
  250.  
  251. unsigned char * pascal
  252. /* XTAG:findNextFile */
  253. findNextFile(dta)
  254.     unsigned char *dta;
  255. {
  256.     extern union REGS rin, rout;
  257.  
  258.     /* set the disk tranfer address */
  259.     rin.h.ah = 0x1A;
  260.     rin.x.dx = (int)dta;
  261.     intdos(&rin, &rout);
  262.  
  263.     /* find the next matching file name */
  264.     rin.h.ah = 0x4F;
  265.     intdos(&rin, &rout);
  266.  
  267.     if( (rout.x.cflag & 1) == 1 )
  268.         return NULL;
  269.     else
  270.         return dta;
  271. }
  272.  
  273.  
  274. int pascal
  275. /* XTAG:findTag */
  276. findTag(fn)
  277.     int fn;
  278. {
  279.     extern struct window *selWindow;
  280.     extern long selBegin, selEnd;
  281.     extern int selMode;
  282.     extern unsigned char msgBuffer[];
  283.     extern unsigned char textBuffer[];
  284.     extern unsigned char tagPattern[];
  285.     extern unsigned char tagMarker[];
  286.     extern unsigned char lastTag[STRINGSIZE];
  287.     extern unsigned char tagDTA[64];
  288.     extern long tagLastCp;
  289.     extern int tagLastLine;
  290.     extern int scrRows, scrCols;
  291.     extern int menuLine;
  292.     extern int ignoreCase;
  293.  
  294.     struct window *w;
  295.     register unsigned char  *p;
  296.     unsigned char tagString[STRINGSIZE], *filename, ch;
  297.     int fileId, linesPassed, markerLength;
  298.     long cp, fSize, startCp;
  299.     int row1, row2, col1, col2;
  300.     int ret, saveIgnoreCase, totalLinesPassed;
  301.  
  302.     /* get the string to search for */
  303.     if( fn == FTAGSEL || fn == FCTAGSEL ) {
  304.         getSelection(tagString);
  305.     } else {    /* fn = FTAG || fn == FCTAG */
  306.         filename = getInput("Tag name: ", lastTag, 0);
  307.         if( filename == NULL ) {
  308.             msg("Find tag cancelled", 1);
  309.             ret = 2;
  310.             goto doReturn;
  311.         }
  312.         strncpy(tagString, filename, STRINGSIZE);
  313.     }
  314.  
  315.     /* adjust for ignoring case by forcing all lower case */
  316.     if( ignoreCase != 0 ) {
  317.         p = tagString